const Task = require('../models/task')
const Journal = require('../models/journal')
const User = require('../models/user')
const Category = require('../models/category')
const Notice = require('../models/notice')

const response = require('../utils/response')
const loop = require('../utils/loop')
const fs = require('fs')
let path = require('path')
const task = require('../models/task')
const {
  body
} = require('express-validator')

const {huanTime} = require('../utils/tool')

//添加日志
exports.journalCreate = async (req, res, next) => {
  try {
    let oldData = req.oldData //返回的旧数据
    let newData = req.newData //返回的新数据

    let journal = new Journal()

    if (!newData){
      //删除操作
      
      oldData && journal.oldData.push(oldData)
      newData && journal.newData.push(newData)

      journal.userId = req.body.userId
      // journal.creator = newData.creator
      journal.worker = oldData.worker
      journal.operate = req.operate
      journal.taskId = oldData._id
      journal.taskname = oldData.taskname

      if (req.operate === 3) { //删除加入消息表
        let notic = new Notice()
        notic.userId = req.body.userId
        notic.notifier = oldData.worker
        notic.operate = req.operate

        notic.tips = "您的任务 <h3>" + oldData.taskname + "</h3> 已被删除"

        await notic.save()
      }

      await journal.save()

      response(res, 200, '删除成功', {success:true})
      return
    }

    oldData && journal.oldData.push(oldData)
    newData && journal.newData.push(newData)

    journal.userId = req.body.userId
    // journal.creator = newData.creator
    journal.worker = newData.worker
    journal.operate = req.operate
    journal.taskId = newData._id
    journal.taskname = newData.taskname

    // 更正时间
    journal.createdAt = huanTime(Date.now())
    journal.updatedAt = huanTime(Date.now())

    let notic = new Notice()
    // if (req.operate === 4){ //审核通过加入消息表
    //   notic.notifier = newData.worker
    
    //   notic.tips = "您的任务 <h3>"+newData.taskname+"</h3> 审核已通过"

    // }
    if (req.operate === 5){ //审核打回加入消息表
      notic.notifier = newData.worker

      notic.tips = newData.taskname + "您提交的文件有被打回"

    }
    if (req.operate === 1){ //任务更新加入消息表
      notic.notifier = newData.worker

      notic.tips = "您的任务 <h3>"+newData.taskname+"</h3> 已经更新信息，请及时查看"

    }
    if (req.operate === 2){ //新增任务加入消息表
      notic.notifier = newData.worker

      notic.tips = "您有新任务 <h3>"+newData.taskname+"</h3> ,请及时查看"

    }
    // if (req.operate === 0){ //提交加入消息表
    //   notic.notifier = newData.creator

    //   notic.tips = "任务 <h3>"+newData.taskname+"</h3> 已提交成功 ,请及时审核"

    // }
    notic.userId = req.body.userId
    notic.operate = req.operate
    notic.taskId = newData._id

    // 更正时间
    notic.createdAt = huanTime(Date.now())
    notic.updatedAt = huanTime(Date.now())

    //添加消息表
    await notic.save()
    
    //添加日志
    await journal.save()
    // res.send(task)
    response(res, 200, '成功', newData)
  } catch (error) {
    next(error)
  }
}

//获取日志通知信息
exports.getJournal = async (req,res,next) => {
  let journals = await Journal.find({operate:3}).lean()
  journals[0].tips = "你的任务<h3>"+journals[0].taskname+"</h3>已被删除"
  res.send(journals)
}